package com.lagou.controller;

import com.lagou.pojo.Article;
import com.lagou.pojo.PageInfo;
import com.lagou.pojo.PageRequest;
import com.lagou.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Calendar;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class IndexController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping()
    public String toIndexPage(Model model, PageRequest pageRequest){
        Page<Article> content = articleService.list(pageRequest.getPage(), pageRequest.getSize());
        PageInfo<Article> pageInfo = new PageInfo<>(pageRequest.getPage(), pageRequest.getSize(), content);
        model.addAttribute("pageInfo", pageInfo);
        return "index";
    }

    @RequestMapping(value = "/findById")
    @ResponseBody
    public Article findById(Article article) {
        return articleService.findById(article);
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public int update() {
        return articleService.update("测试更新", 1);
    }

}
